<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="变更编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['changeCode']" placeholder="变更编号"
                       disabled="true"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="专题名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['subjectName', validatorRules.subjectName]"
                placeholder="请选择专题名称"
                @click="subjectIdClick"
              ></a-input>
              <a-input v-decorator="['subjectId']" v-show='false'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="项目简称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['shortName']" disabled></a-input>
              <a-input v-decorator="['projectId']" v-show='false'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="变更类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['changeType', validatorRules.changeType]"
                                 :trigger-change="true" dictCode="hf_project_change_types" placeholder="请选择变更类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="项目延期/天" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['projectDelay', validatorRules.projectDelay]" placeholder="请输入项目延期/天"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="专题延期/天" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['subjectDelay', validatorRules.subjectDelay]" placeholder="请输入专题延期/天"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="人天成本" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['countPrice', validatorRules.countPrice]" placeholder="请输入人天成本"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['changeState', validatorRules.changeState]"
                                 :trigger-change="true" dictCode="hf_project_change_states" placeholder="请选择状态"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="变更人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep v-decorator="['costWorkday']" disabled="true"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="变更时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择变更时间" v-decorator="['changeTime']"
                      :trigger-change="true" style="width: 100%" disabled="true"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="确认人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep :multi="false" v-decorator="['confirmorId']"></j-select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="确认时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date v-decorator="['confirmorTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="确认意见" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['confirmorDes']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep :multi="false" v-decorator="['createBy']"></j-select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date v-decorator="['createTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="变更描述" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['changeDes', validatorRules.changeDes]" rows="4" placeholder="请输入变更描述"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['remarks']" rows="4" placeholder="请输入备注"/>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
        <hf-pm-project-subjects-select-modal ref="hfPmProjectSubjectsSelectModal" :multiple="false"
                                             @hf-pm-project-subjects="hfPmProjectsubjects"/>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import HfPmProjectSubjectsSelectModal from './HfPmProjectSubjectsSelectModal'

  export default {
    name: 'HfPmProjectChangeRecordsForm',
    components: {
      HfPmProjectSubjectsSelectModal
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: () => {
        },
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 }
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: '/pm/hfPmProjectChangeRecords/add',
          edit: '/pm/hfPmProjectChangeRecords/edit',
          queryById: '/pm/hfPmProjectChangeRecords/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return true
          }
        }
        return false
      }
    },
    created() {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData()
    },
    methods: {
      subjectIdClick() {
        this.$nextTick(() => {
          this.$refs.hfPmProjectSubjectsSelectModal.title = '选择专题'
          this.$refs.hfPmProjectSubjectsSelectModal.visible = true
        })
      },
      hfPmProjectsubjects(data) {
        let myDate = new Date()
        let b = (myDate.getMonth() + 1).toString()
        if (b.length == 1) {
          b = '0' + b
        }
        let a = myDate.getFullYear().toString() + b + myDate.getDate().toString() + myDate.getHours().toString() + myDate.getMinutes().toString() + myDate.getSeconds().toString()
        this.form.setFieldsValue({
          subjectId: data.id,
          subjectName: data.subjectName,
          projectId: data.projectId,
          shortName: data.projectName,
          changeCode: data.subjectCode + '-BG-' + a,
          costWorkday: this.$ls.get('Login_Userinfo').username,
          changeTime: new Date().toLocaleDateString().replaceAll('/', '-')
        })
      },
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'changeCode', 'subjectId', 'subjectName', 'projectId', 'shortName',
            'changeType', 'changeDes', 'projectDelay', 'subjectDelay', 'countPrice', 'changeState',
            'remarks', 'costWorkday', 'changeTime', 'confirmorId', 'confirmorTime', 'confirmorDes', 'createBy', 'createTime'))
        })
      },
      //渲染流程表单数据
      showFlowData() {
        if (this.formBpm === true) {
          let params = { id: this.formData.dataId }
          getAction(this.url.queryById, params).then((res) => {
            if (res.success) {
              this.edit(res.result)
            }
          })
        }
      },
      submitForm() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            console.log('表单提交数据', formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          }

        })
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'changeCode', 'subjectName', 'projectId', 'shortName', 'changeType', 'changeDes', 'projectDelay', 'subjectDelay', 'countPrice', 'changeState', 'remarks', 'costWorkday', 'changeTime', 'confirmorId', 'confirmorTime', 'confirmorDes', 'createBy', 'createTime'))
      }
    }
  }
</script>